import scrapy
from scrapy_03_movie.items import Scrapy03MovieItem


class MovieSpider(scrapy.Spider):
    name = "movie"
    allowed_domains = ["www.dy2018.com"]
    start_urls = ["https://www.dy2018.com/2/"]

    def parse(self, response):
        # //div[@class="co_content8"]//td[2]//a[2]/@href
        # //div[@class="co_content8"]//td[2]//a[2]/text()

        a_list: list[scrapy.Selector] = response.xpath('//div[@class="co_content8"]//td[2]//a[2]')
        print(a_list)
        for a in a_list:
            name = a.xpath('./text()').extract_first()
            href = a.xpath('./@href').extract_first()
            # print(name, src)

            url = 'https://www.dy2018.com' + href
            print(url)
            # 访问
            # meta 传递数据
            yield scrapy.Request(url=url, callback=self.parse_second, meta={'name': name})

    def parse_second(self, response):
        # print("parse_second")
        src = response.xpath('//div[@id="Zoom"]/img[1]/@src').extract_first()
        name = response.meta['name']
        movie = Scrapy03MovieItem(src=src, name=name)
        yield movie
